package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.services.ISignService;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/sign")
public class SignController {
    @Autowired
    private ISignService signService;
    @RequestMapping("/isSignedToday")
    public AppResult<Boolean> isSignedToday(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        return AppResult.success(signService.hasSignedToday(user.getUser().getId()));
    }
    @RequestMapping("/getSignDays")
    public AppResult<Map<String,Object>> getSignDays(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        long totalDays = signService.getTotalSignedDays(user.getUser().getId());
        int continuousDays = signService.getCurrentConsecutiveDays(user.getUser().getId());
        Map result = new HashMap<>();
        result.put("totalDays",totalDays);
        result.put("continuousDays",continuousDays);
        return AppResult.success(result);
    }
    @RequestMapping("/sign")
    public AppResult<Boolean> sign(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        signService.sign(user.getUser().getId());
        return AppResult.success(true);
    }
}
